package pagePrint;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.jsp.JspWriter;

import channel.Channel;
import channel.ChannelTime;
import database.Database;

public class pagePrintThis {
	//print channel message while user create a THIS channel in "this.jsp"
	public static void Print(JspWriter out, int id)
	{
		try {
			Connection con = Database.getConnection();
			Statement stmt = con.createStatement();
			
			ResultSet rs = stmt.executeQuery(String.format(
						"select tag, pic_url " +
						"from channel " +
						"where id = %d",
						id));
			
			out.println("<div><div class='step_title'>");
			out.print("<img src=");
			
			rs.next();
			out.print("'" + rs.getString("pic_url") + "'");
			out.print(" title = ");
			out.println("'" + rs.getString("tag") + "' style='height:30; width:30'/>");
			out.println("<span class='step_t'>Choose a trigger</span>" + 
						"<span class='of_step'>step 2 of 7</span>");
			out.println("<div class='clear'></div></div></div>");
			
			stmt.close();
			con.close();
			
			if(id == Channel.DATE_TIME)
				PrintTime(out);
			else if(id == Channel.WEIBO)
				PrintWeibo(out);
			else if(id == Channel.EMAIL)
				PrintMail(out);
			else if(id == Channel.YMAIL)
				PrintYmail(out);
			else if(id == Channel.TIMEDOUTWEIBO)
				PrintTimedoutWeibo(out);
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void PrintTimedoutWeibo_Deep(JspWriter out, int extra)
	{
		try{
			out.println("<div><class = 'clear'></div>");
			out.println("<form method='POST' action='createtaskThis'>");
			out.println("<input type='hidden' name='thisid' value='5'/>");
			out.println("<p>From Screen Name<br><input type='text' name='target' size='20'></p>");
			if(extra == 0)
				out.println("<input type='hidden' name='key' value=''/>");
			else
				out.println("<p>key<br><input type='text' name='key' size='20'></p>");
			
			//end date
			out.println("<p>End Date<br></p>");
			//month
			out.println("<p><select size='1' name='eMonth'>");
			out.println("<option selected>1</option>");
			for(int i = 2; i < 13; i++)
				out.println("<option>" + i + "</option>");
			out.println("</select>Month");
			//day
			out.println("<select size='1' name='eDay'>");
			out.println("<option selected>1</option>");
			for(int i = 2; i < 32; i++)
				out.println("<option>" + i + "</option>");
			out.println("</select>Day");
			//hour
			out.println(" <select size='1' name='eHour'>");
			out.println("<option selected>0</option>");
			for(int i = 1; i < 24; i++)
				out.println("<option>" + i + "</option>");
			out.println("</select>");
			//min
			out.println(" : <select size='1' name='eMin'>");
			out.println("<option selected>00</option>");
			for(int i = 1; i < 60; i++)
				out.println("<option>" + i + "</option>");
			out.println("</select></p>");
			
			out.println("<p><div class = 'big_btn'><input type='submit' value='ok' name='B1'/></div></p>");
			out.println("</form>");
			
			
			out.flush();
		}
		catch(IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void PrintYmail_Deep(JspWriter out, int extra)
	{
		try{
			out.println("<div><class = 'clear'></div>");
			out.println("<form method='POST' action='createtaskThis'>");
			out.println("<input type='hidden' name='thisid' value='4'/>");
			
			out.println("<p>user<br><input type='text' name='user' size='20'>@");
			out.println("<select size='1' name='mailType'>");
			out.println("<option selected>163.com</option>");
			out.println("<option>126.com</option>");
			out.println("<option>yeah.net</option>");
			out.println("</select></p>");
			out.println("<p>pwd<br><input type='password' name='pwd' size='20'></p>");
			out.println("<p>from<br><input type='text' name='from' size='20'></p>");
			if(extra == 0)
				out.println("<input type='hidden' name='key' value=''/>");
			else
				out.println("<p>key<br><input type='text' name='key' size='20'></p>");
			out.println("<p><div class = 'big_btn'><input type='submit' value='ok' name='B1'/></div></p>");
			out.println("</form>");
			
			out.flush();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void PrintWeibo_Deep(JspWriter out, int extra)
	{
		try{
			out.println("<div><class = 'clear'></div>");
			out.println("<form method='POST' action='createtaskThis'>");
			out.println("<input type='hidden' name='thisid' value='2'/>");
			out.println("<p>From Screen Name<br><input type='text' name='target' size='20'></p>");
			if(extra == 0)
				out.println("<input type='hidden' name='key' value=''/>");
			else
				out.println("<p>key<br><input type='text' name='key' size='20'></p>");
			out.println("<p><div class = 'big_btn'><input type='submit' value='ok' name='B1'/></div></p>");
			out.println("</form>");
			
			out.flush();
		}
		catch(IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void PrintMail_Deep(JspWriter out, int extra)
	{
		try{
			out.println("<div><class = 'clear'></div>");
			out.println("<form method='POST' action='createtaskThis'>");
			out.println("<input type='hidden' name='thisid' value='3'/>");
			out.println("<p>from<br><input type='text' name='from' size='20'></p>");
			if(extra == 0)
				out.println("<input type='hidden' name='key' value=''/>");
			else
				out.println("<p>key<br><input type='text' name='key' size='20'></p>");
			out.println("<p><div class = 'big_btn'><input type='submit' value='ok' name='B1'/></div></p>");
			out.println("</form>");
			
			out.flush();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void Print_Deep(JspWriter out, int id, int extra) throws IOException, SQLException
	{
		Connection con = Database.getConnection();
		Statement stmt = con.createStatement();
		
		ResultSet rs = stmt.executeQuery(String.format(
					"select tag, pic_url " +
					"from channel " +
					"where id = %d",
					id));
		
		
		out.println("<div class='clear'></div><br>");
		out.println("<div><div class='step_title'>");
		out.print("<img src=");
		
		rs.next();
		out.print("'" + rs.getString("pic_url") + "'");
		out.print(" title = ");
		out.println("'" + rs.getString("tag") + "' style='height:30; width:30'/>");
		out.println("<span class='step_t'>Complete trigger fields</span>" + 
					"<span class='of_step'>step 3 of 7</span>");
		out.println("<div class='clear'></div></div></div>");
		
		stmt.close();
		con.close();
		
		if(id == Channel.DATE_TIME)
			PrintTime_Deep(out, extra);
		else if(id == Channel.WEIBO)
			PrintWeibo_Deep(out, extra);
		else if(id == Channel.EMAIL)
			PrintMail_Deep(out, extra);
		else if(id == Channel.YMAIL)
			PrintYmail_Deep(out, extra);
		else if(id == Channel.TIMEDOUTWEIBO)
			PrintTimedoutWeibo_Deep(out, extra);
	}
	
	static void PrintWeibo(JspWriter out)
	{
		try {
			out.print("<br>");
			out.println("<div><class = 'clear'></div>");
			for(int i = 0; i < 2; i++)
			{
				out.print("<a href='createtaskThis?thisid=2&everyid=" + i + "'>");
				out.print("<div class='individual_trigger'>");
				//title
				if(i == 0)
					out.println("<span class='trigger_t'>Weibo from</span>");
				else
					out.println("<span class='trigger_t'>Weibo from with key</span>");
				
				//detail
				out.println("<br><span class='trigger_d'>");
				if(i == 0)
					out.println("This trigger fires every time the sina weibo\n" + 
							"user you specify update an status.");
				else
					out.println("This trigger fires every time you the\n" + 
							"user update an status with key.");
				
				out.println("</span></div></a>  ");
			}
			out.flush();
		}
		
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static void PrintMail(JspWriter out)
	{
		try {
			out.print("<br>");
			out.println("<div><class = 'clear'></div>");
			for(int i = 0; i < 2; i++)
			{
				out.print("<a href='createtaskThis?thisid=3&everyid=" + i + "'>");
				out.print("<div class='individual_trigger'>");
				//title
				if(i == 0)
					out.println("<span class='trigger_t'>Send IFTTT any email</span>");
				else
					out.println("<span class='trigger_t'>Send IFTTT an email key</span>");
				
				//detail
				out.println("<br><span class='trigger_d'>");
				if(i == 0)
					out.println("Send ifttt any email at njucs_trigger@yeah.net\n" + 
							"and this trigger fires.");
				else
					out.println("Send ifttt an email at njucs_trigger@yeah.net\n" + 
							"with a key in the subject and this trigger fires.");
				
				out.println("</span></div></a>  ");
			}
			out.flush();
		}
		
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static void PrintYmail(JspWriter out)
	{
		try {
			out.print("<br>");
			out.println("<div><class = 'clear'></div>");
			for(int i = 0; i < 2; i++)
			{
				out.print("<a href='createtaskThis?thisid=4&everyid=" + i + "'>");
				out.print("<div class='individual_trigger'>");
				//title
				if(i == 0)
					out.println("<span class='trigger_t'>New email from</span>");
				else
					out.println("<span class='trigger_t'>New email from with key</span>");
				
				//detail
				out.println("<br><span class='trigger_d'>");
				if(i == 0)
					out.println("This trigger fires every time you receive a\n" + 
							"new email from an address you specify.");
				else
					out.println("This trigger fires every time you receive a\n" + 
							"new email from an address you specify that \n" + 
							"matches a simple search query in the subject \n" + 
							"of the email.");
				
				out.println("</span></div></a>  ");
			}
			out.flush();
		}
		
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static void PrintTimedoutWeibo(JspWriter out)
	{
		try {
			out.print("<br>");
			out.println("<div><class = 'clear'></div>");
			for(int i = 0; i < 2; i++)
			{
				out.print("<a href='createtaskThis?thisid=5&everyid=" + i + "'>");
				out.print("<div class='individual_trigger'>");
				//title
				if(i == 0)
					out.println("<span class='trigger_t'>No Weibo in time</span>");
				else
					out.println("<span class='trigger_t'>No Weibo with key in time</span>");
				
				//detail
				out.println("<br><span class='trigger_d'>");
				if(i == 0)
					out.println("This trigger fires if the user\n" + 
							"not update an status in time.");
				else
					out.println("This trigger fires if the user\n" + 
							"not update an status with key in time.");
				
				out.println("</span></div></a>  ");
			}
			out.flush();
		}
		
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static void PrintTime(JspWriter out)
	{
		try {
			out.print("<br>");
			out.println("<div><class = 'clear'></div>");
			for(int i = 0; i < 4; i++)
			{
				out.print("<a href='createtaskThis?thisid=1&everyid=" + i + "'>");
				out.print("<div class='individual_trigger'>");
				//title
				if(i == 0)
					out.println("<span class='trigger_t'>Every hour at</span>");
				else if(i == 1)
					out.println("<span class='trigger_t'>Every day at</span>");
				else if(i == 2)
					out.println("<span class='trigger_t'>Every mouth on</span>");
				else
					out.println("<span class='trigger_t'>Every year on</span>");
				
				//detail
				out.println("<br><span class='trigger_d'>");
				if(i == 0)
					out.println("This trigger fires once an hour at :00, :15\n:30, or :45 minutes past the hour.");
				else if(i == 1)
					out.println("This trigger fires every single day at a\nspecific time set by you.");
				else if(i == 2)
					out.println("This trigger fires every month on the day\nand time you specify.");
				else
					out.println("This trigger fires once a year on the date\nand time you specify.");
				
				out.println("</span></div></a>  ");
			}
			out.flush();
		}
		
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static void PrintTime_Deep(JspWriter out, int extra)
	{
		try{
			out.println("<br>");
			out.println("<div><class = 'clear'></div>");
			out.println("<form method='POST' action='createtaskThis'>");
			out.println("<input type='hidden' name='thisid' value='1'/>");
			out.println("<input type='hidden' name='everyid' value='" + extra + "'/>");
			out.println("<span style='font-size:12px'>Date and time<br></span>");
			
			if(extra >= ChannelTime.EVERY_YEAR)
			{
				out.println("<select size='1' name='Month'>");
				out.println("<option selected>1</option>");
				for(int i = 2; i < 13; i++)
					out.println("<option>" + i + "</option>");
				out.println("</select>Month");
			}
			
			if(extra >= ChannelTime.EVERY_MONTH)
			{
				out.println("<select size='1' name='Day'>");
				out.println("<option selected>1</option>");
				for(int i = 2; i < 32; i++)
					out.println("<option>" + i + "</option>");
				out.println("</select>Day -- ");
			}
			
			if(extra >= ChannelTime.EVERY_DAY)
			{
				out.println("<select size='1' name='Hour'>");
				out.println("<option selected>0</option>");
				for(int i = 1; i < 24; i++)
					out.println("<option>" + i + "</option>");
				out.println("</select> : ");
			}
			
			out.println("<select size='1' name='Min'>");
			out.println("<option selected>00</option>");
			for(int i = 1; i < 4; i++)
				out.println("<option>" + i*15 + "</option>");
			out.println("</select>");
			
			out.println("<br><div class = 'big_btn'><input type='submit' value='ok' name='B1'/></div>");
			out.println("</form>");
			
			out.flush();
		}

		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
